package com.codefish.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codefish.dto.RoleDto;
import com.codefish.entity.SysRoleEntity;
import com.codefish.service.SysRoleService;
import com.codefish.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Classname SysRoleController
 * @Description 角色表
 * @Date 2021/5/3 12:04
 * @Created 学兔斯基眯眼看世界
 */
@RestController
@RequestMapping("/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService roleService;

    /**
     * 保存角色
     *
     * @param roleDto
     */
    @PostMapping("/saveRole")
    public R saveRole(@RequestBody RoleDto roleDto) {
        return roleService.saveRole(roleDto);
    }

    /**
     * 条件分页查询
     *
     * @param name
     * @return
     */
    @GetMapping("/getPageByParams")
    @ResponseBody
    public R getPageByParams(@RequestParam(value = "name", required = false) String name
            , @RequestParam(value = "page", required = true) int page
            , @RequestParam(value = "limit", required = true) int limit) {

        // 条件分页查询
        IPage<SysRoleEntity> entityIPage = roleService.getPageByParams(name, page, limit);

        List<SysRoleEntity> reList = entityIPage.getRecords();

        long total = entityIPage.getTotal();

        return R.ok().put("entityList", reList).put("total", total);
    }

    /**
     * 根据角色主键id查询保存的菜单资源id
     *
     * @param id
     * @return
     */
    @GetMapping("/queryPermissionIdsByRoleId")
    public List<Long> queryPermissionIdsByRoleId(@RequestParam(value = "id", required = true) Long id) {
        return roleService.queryPermissionIdsByRoleId(id);
    }

    /**
     * 逻辑删除
     *
     * @param id
     * @return
     */
    @GetMapping("/deleteOne")
    public R deleteOne(@RequestParam(value = "id", required = true) Long id) {
        roleService.deleteRole(id);
        return R.ok("删除成功！");
    }


}
